# -*- coding: utf-8 -*-
#
# This handles the game, using a huge Game class
#
#

import pygame
from pygame.locals import *
import sys

import default
import data
import background
#import leveljakob as level
import level
import character

class Game:
    def __init__(self, screen):
        """Supplied screen on which to draw"""
        self.screen = screen
        self.camera = pygame.rect.Rect((0,0), self.screen.get_size())
        #self.background = background.Background('background.png')
        
        #print self.background
        
        self.player = character.Player("badrobot.png", (0,0))
        self.playergroup = pygame.sprite.RenderPlain(self.player)
        #NEJ! HAN SKA VARA I MITTEN AV BANAN, INGA SKÄRMKOORDINATER!
        self.player.rect.center = screen.get_rect().center

        self.setup_controls()
                
        
    def really_quit(self):
        """Confirm quit event"""
        print "Fuck it. I quit anyways"
        sys.exit(0)

    def check(self):
        """Checks and handles events according to the
        description in game.Game.setup_controls.
        We also check for events that are beyond the user's
        control."""

        for event in pygame.event.get():
            if event.type == QUIT:
                #We don't wanna quit without asking first. That's rude.
                self.really_quit()

            if event.type == KEYDOWN:
                for item in self.kb_controls.items():
                    #If we have a match in keys
                    if event.key == item[0]:
                        #We tell the according function that we pressed it
                        item[1](True)
            if event.type == KEYUP:
                for item in self.kb_controls.items():
                    #If we have a match in keys
                    if event.key == item[0]:
                        #We tell the according function that we released it
                        item[1](False)
            if event.type == MOUSEBUTTONDOWN:
                #We can only check individual buttons with pygame.mouse
                pressed_buttons = pygame.mouse.get_pressed()
                if pressed_buttons[0]:
                    self.mouse_controls[0](True)
                if pressed_buttons[1]:
                    self.mouse_controls[1](True)
                if pressed_buttons[2]:
                    self.mouse_controls[2](True)
            if event.type == MOUSEBUTTONUP:
                pressed_buttons = pygame.mouse.get_pressed()
                if pressed_buttons[0]:
                    self.mouse_controls[0](False)
                if pressed_buttons[1]:
                    self.mouse_controls[1](False)
                if pressed_buttons[2]:
                    self.mouse_controls[2](False)


    def setup_controls(self):
        """Sets up a dictionary of key:function pairs.
        Every function should be simple, only handling
        things inside this class instance, and requiring
        only an argument saying if the key was pressed or
        released.
        We get a tuple for the mouse, and a dictionary for the
        keyboard, but the principle is the same."""
        self.kb_controls = { K_LEFT : self.player.left,
                             K_RIGHT : self.player.right,
                             K_SPACE : self.player.jetpack
                             }
        self.mouse_controls = ()

    ### The user may use these
    #def left(self, key_press):
    #    """Makes the left go up (yeah, right...)"""
    #    self.scroll((-default.scroll_step, 0))
    #    #self.background.move_right()
    #    
    #def right(self, key_press):
    #    """move background left, to create the illusion of movement"""
    #    self.scroll((default.scroll_step, 0))
    #    #self.background.move_left()
    # 
    #def down(self, key_pressed):
    #    if key_pressed:
    #        print "Now I am going down"
    #    else:
    #        print "I stopped going down"
    ### End of controls

    def scroll(self, distance):
        """Scroll according to distance vector (tuple)"""
        self.camera.move_ip(distance)
    def run(self):
        #self.image, self.image_rect = data.load_image("uglytile.png")

        lv = level.BasicLevel("apa.snopp")
        
        while True:
            self.check()
            #self.scroll((-5*default.scroll_step, 0))
            #self.image_rect.topleft = (self.screen.get_width() / 2 - self.camera.left,\
            #                           self.screen.get_height() / 2 - self.camera.top) 
            #self.background.draw(self.screen)
            self.playergroup.update()
            
            lv.draw(self.camera, self.screen)
            self.playergroup.draw(self.screen)
            
            #print self.camera
            pygame.display.flip()
